package com.steakliu.sun.scanner;

import com.steakliu.sun.annotation.SunReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2022-12-10  19:00
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class SunReferenceScanner extends ClassScanner{
  private static final Logger LOGGER = LoggerFactory.getLogger(SunReferenceScanner.class);

  public static Map<String, Object> scanner(String host, int port , String scanPackage) throws Exception {
    Map<String, Object> handlerMap = new HashMap<>();
    List<String> classNameList = getClassNameList(scanPackage);
    if (classNameList.isEmpty()){
      return handlerMap;
    }
    for (String className : classNameList) {
      Class<?> clazz = Class.forName(className);
      Field[] declaredFields = clazz.getDeclaredFields();
      Stream.of(declaredFields).forEach(field -> {
        SunReference rpcReference = field.getAnnotation(SunReference.class);
        if (rpcReference != null){
          LOGGER.info("SunReference  ==> {}", rpcReference);
        }
      });
    }
    return handlerMap;
  }
}
